<?php
class OLD_Unit_1T extends Unit_Base{ 
/*
 * 	ONE TILE SIZED UNIT MODEL
 * 	Marc Binns 20/05/2010
 */


	/**
	 * Creates a new 1T unit and returns its details
	 * 
	 * @return 	Array Of [Success (If Success is False unit creation failed. Rest of Results array will be null/missing!), Array of Tile ids, MovedLastTurn, BattleId, PlayerId, New_UnitId, TeamName, UnitTypeId, New_Footprint, New_UnitAgility, New_WeaponId_1,2,3,4,5,6, New_HP, New_SP, New_MoveAP, New_AttackAP, StartingBearing, StartingCoord_X, StartingCoord_Y, RemainingAmmo_1,2,3,4,5,6]  
	 */
	public function create1TUnit($battleId, $playerId, $unitTypeId, $startWithAP, $coord_X, $coord_Y, $bearing){
		
		Try{
			//Attempt Unit creation in DB
			$params = array($battleId, $playerId, $unitTypeId, $startWithAP, $coord_X, $coord_Y, $bearing);
			$db = Database::getInstance();
			$row = $db->callSP(GameConfig::sP_Unit1T_Create, $params);
			
			If (strval($row['Success']) == "N"){
				//FAILED
				return false;
				
			}Else{
				//SUCCESS!
				//True = Success
				//array("1"=>"1") = Tile ids
				//MovedLastTurn - Marc: Always true?
				$result = array(
				True,
				array("1"=>"1"), 
				false, 
				intval($row['BattleId']),
				intval($row['PlayerId']),
				intval($row['New_UnitId']),
				strval($row['New_Callsign']),
				strval($row['TeamName']),
				intval($row['UnitTypeId']),
				intval($row['New_Footprint']),
				intval($row['New_UnitAgility']),
				intval($row['New_WeaponId_1']),
				intval($row['New_WeaponId_2']),
				intval($row['New_WeaponId_3']),
				intval($row['New_WeaponId_4']),
				intval($row['New_WeaponId_5']),
				intval($row['New_WeaponId_6']),
				intval($row['New_HP']),
				intval($row['New_SP']),
				intval($row['New_MoveAP']),
				intval($row['New_AttackAP']),
				intval($row['StartingBearing']),
				intval($row['StartingCoord_X']),
				intval($row['StartingCoord_Y']),
				intval($row['RemainingAmmo_1']),
				intval($row['RemainingAmmo_2']),
				intval($row['RemainingAmmo_3']),
				intval($row['RemainingAmmo_4']),
				intval($row['RemainingAmmo_5']),
				intval($row['RemainingAmmo_6']));

				//SUCCESS!				
				return $result;
			}
		}
		catch(Exception $e){
			// @todo MARC: Tie Unit_1T exception Handling into common Handler
  			echo 'ERROR: ' .$e->getMessage();
			return false;
		}
	}
	/**
	 * Move a 1T unit to new coordinates 
	 */
	public function move1TUnit($uId, $Nx, $Ny, $b){
		require_once(DirectoryManager::$pathCore."/Database.php");
		Try{
			//Attempt DB Unit move
			//BattleId, PlayerId, UnitId, NewBearing, NewX, NewY
			$params = array($uId, $b, $Nx, $Ny);
			$db = Database::getInstance();
	 		$row = $db->callSP(GameConfig::sP_Unit1T_Move, $params);
	 		
	 		//Check if the move was successful
	 		If (strval($row['S']) == 1 ){
	 			return true;
	 		}ElseIf(strval($row['S']) == -1){
	 			return false;
	 		}
	 		Else{
	 			return false;
	 		}
		}catch(Exception $e){
			// @todo MARC: Tie Unit_1T exception Handling into common Handler
			//FAILED!
  			echo 'ERROR: ' .$e->getMessage();
  			return false;
		}
	}
	/**
	 * Attack target(s) using the specified weapon of this 1T Unit (Returns true if successful)
	 *
	 * @param int $targetX
	 * @param int $targetY
	 * @param int $targetUnitId
	 * @param int $firingWeaponId
	 */
	public function attack($targetX, $targetY, $targetUnitId, $firingWeaponId){
		// @todo MARC: Unit_1T Attack!
		// @todo MARC: Talk with Matt - How do we want to handle updates to attacked units (i.e. targets of any attack)
		// @todo MARC: Talk with Matt - Do we need to have different Attack functions for different footprint units?
		
	}
	
	
	
	
//	=== OLD ===================================================
///**
//	 * Creates a new Unit in a given battle (Returns true if successful)
//	 *
//	 * @param int $unitTypeId
//	 * @param int $battleId
//	 * @param int $playerId
//	 * @param int $coord_X
//	 * @param int $coord_Y
//	 * @param string $startWithAP - Y or N
//	 * @return boolean
//	 */
//	public function __construct($unitTypeId, $battleId, $playerId, $coord_X, $coord_Y, $startWithAP, $bearing){
//		Try{
//		//Create DB unit and get details
//		$params = array($battleId, $playerId, $unitTypeId, $startWithAP, $coord_X, $coord_Y, $bearing);
//		$db = Database::getInstance();
// 		$row = $db->callSP(GameConfig::sP_Unit1T_Create, $params);
// 		
// 		//Construct the Unit object
// 		// @todo MARC: Ask matt what happens when using intval on a null return field!
// 		// @todo MARC: We need a select/insert method, current SP will run into Id issues due to timing!
// 		$this->battle_id = intval($row['BattleId']);
// 		$this->player_id = intval($row['PlayerId']);
// 		$this->unit_id = intval($row['New_UnitId']);
// 		$this->teamName = strval($row['TeamName']);
// 		$this->unitType_Id = intval($row['UnitTypeId']);
// 		$this->footprint = intval($row['New_Footprint']);
// 		$this->unitAgility = intval($row['New_UnitAgility']);
// 		$this->weaponType_Id1 = intval($row['New_WeaponId_1']);
// 		$this->weaponType_Id2 = intval($row['New_WeaponId_2']);
// 		$this->weaponType_Id3 = intval($row['New_WeaponId_3']);
// 		$this->unitTile_Ids = array("1"=>"1"); //HARDCODED
// 		$this->current_HP = intval($row['New_HP']);
// 		$this->current_SP = intval($row['New_SP']);
// 		$this->current_MoveAP = intval($row['New_MoveAP']);
// 		$this->current_AttackAP = intval($row['New_AttackAP']);
// 		$this->current_Bearing = intval($row['StartingBearing']);
// 		$this->current_X = intval($row['StartingCoord_X']);
// 		$this->current_Y = intval($row['StartingCoord_Y']);
// 		$this->movedLastTurn = false; //HARDCODED Marc: Always true?
// 		$this->weapon1_Ammo = intval($row['RemainingAmmo_1']);
// 		$this->weapon2_Ammo = intval($row['RemainingAmmo_2']);
// 		$this->weapon3_Ammo = intval($row['RemainingAmmo_3']);
// 		 
// 		//Check if we succeeded
// 		If ($this->unit_Id <> null){
// 			return true;	
// 		}else{
// 			return false;
// 		}
// 		
//		}catch(Exception $e){
//			// @todo MARC: Tie Unit_1T exception Handling into common Handler
//  			echo 'ERROR: ' .$e->getMessage();
//  			return false;
//		}
//	}
//	
//	
//	
//	
//	/**
//	 * Move the 1T Unit (Returns True if successful move)
//	 *
//	 * @param int $new_X
//	 * @param int $new_Y
//	 * @param int $new_Bearing
//	 * @return boolean
//	 */
//	public function move($unitId, $new_X, $new_Y, $new_Bearing){
//		Try{
//		//Attempt DB Unit move
//		//BattleId, PlayerId, UnitId, NewBearing, NewX, NewY
//		$params = array($battleId, $playerId, $unitId, $new_Bearing, $new_X, $new_Y);
//		$db = Database::getInstance();
// 		$row = $db->callSP(GameConfig::sP_Unit1T_Move, $params);
//
// 		
// 		
// 		
// 		//Check if the move was successful
// 		$new_MoveAP = intval($row["Move_AP"]);
// 		If ($this->current_MoveAP = $new_MoveAP){
// 			//Move Failed!
// 			return false;
// 		}
// 		
// 		//Move_AP, Unit_Tile_Bearing, Battle_Coord_X, Battle_Coord_Y
//		$this->current_MoveAP = $new_MoveAP;
// 		$this->current_Bearing = intval($row["Unit_Tile_Bearing"]);
//		$this->current_X = intval($row["Battle_Coord_X"]);
//		$this->current_Y = intval($row["Battle_Coord_Y"]);
//
//		//Move Successful!
//		return true;
//		
//		}catch(Exception $e){
//			// @todo MARC: Tie Unit_1T exception Handling into common Handler
//  			echo 'ERROR: ' .$e->getMessage();
//  			return false;
//		}
//	}
//	/**
//	 * Attack target(s) using the specified weapon of this 1T Unit (Returns true if successful)
//	 *
//	 * @param int $targetX
//	 * @param int $targetY
//	 * @param int $targetUnitId
//	 * @param int $firingWeaponId
//	 */
//	public function attack($targetX, $targetY, $targetUnitId, $firingWeaponId){
//		// @todo MARC: Unit_1T Attack!
//		// @todo MARC: Talk with Matt - How do we want to handle updates to attacked units (i.e. targets of any attack)
//		// @todo MARC: Talk with Matt - Do we need to have different Attack functions for different footprint units?
//		
//	}
}
?>